package com.paw.portal.controller;


import java.util.List;

import javax.validation.Valid;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.paw.common.domain.Result;
import com.paw.common.util.BeanUtilTools;
import com.paw.core.annotation.PreAuthorize;
import com.paw.portal.entity.SysUser;
import com.paw.portal.request.dto.SysUserRequestDto;
import com.paw.portal.request.dto.UserSetRoleRequestDto;
import com.paw.portal.response.dto.SysUserResponseDto;
import com.paw.portal.service.IAntdUserService;
import com.paw.portal.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Rubble
 * @since 2021-08-21
 */
@Api(tags = "系统用户信息控制器")
@RestController
public class SysUserController {

  @Autowired
  private ISysUserService sysUserService;

  @Autowired
  private IAntdUserService antdUserService;

  @PreAuthorize("sys:user:view")
  @ApiOperation("用户分页列表")
  @PostMapping("/api/portal/sysUser/pageList")
  public Result<List<SysUserResponseDto>> getSysUserPageList(SysUserRequestDto requestDto){
    return antdUserService.getSysUserPageList(requestDto);
  }

  @ApiOperation("新增用户")
  @PostMapping("/api/portal/sysUser/addUser")
  public Result<SysUserResponseDto> addSysUser(SysUserRequestDto requestDto){
    SysUser entity = new SysUser();
    BeanUtils.copyProperties(requestDto,entity);
    entity.setId(null);
    sysUserService.save(entity);
    SysUserResponseDto responseDto = new SysUserResponseDto();
    BeanUtils.copyProperties(entity,responseDto);
    return Result.success(responseDto);
  }

  @ApiOperation("修改用户")
  @PostMapping("/api/portal/sysUser/updateUser")
  public Result<SysUserResponseDto> updateUser(SysUserRequestDto requestDto){
    Assert.notNull(requestDto.getId(),"id required");
    SysUser entity = new SysUser();
    BeanUtils.copyProperties(requestDto,entity);
    entity.setId(null);
    sysUserService.updateById(entity);
    SysUserResponseDto responseDto = new SysUserResponseDto();
    BeanUtils.copyProperties(entity,responseDto);
    return Result.success(responseDto);
  }

  @ApiOperation("设置角色")
  @PostMapping("/api/portal/sysUser/setRoles")
  public Result<Boolean> setRoles(@Valid @RequestBody UserSetRoleRequestDto requestDto){

    return antdUserService.setUserRole(requestDto);
  }



}
